package indi.gao.redis;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>Description:</p>
 * <p>Copyright: Copyright (c)2019$</p>
 * <p>Company: Tope</p>
 * <P>Created Date :2019$-08$-16$</P>
 * <P>@version 1.0</P>
 *
 * @author gaolei
 */
class Stu {
    private String name;
    private int age;
    private Long ID;
    private Double score;
    private int height;

    @Override
    public String toString() {
        return "Stu{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", ID=" + ID +
                ", score=" + score +
                ", height=" + height +
                '}';
    }
}

public class MaptoObject {
    private static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
        if (map == null) {
            return null;
        }
        Object obj = beanClass.newInstance();
        //得到参数类型个参数名称
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            //判断修饰符
            int mod = field.getModifiers();
            //判断该字段是否被static或者final属性修饰 静态字段不设置值
            if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                continue;
            }
            //the reflected object should suppress Java language access
            field.setAccessible(true);
            //对于map中没有的值，不进行set动作 从而保留数据类型的默认值
            if (map.get(field.getName()) != null) {
                try {
                    //类型可能不对应
                    field.set(obj, map.get(field.getName()));
                } catch (Exception e) {
                    System.out.println(
                            "/**********************************************************************************************/" +
                                    "\n" + e.toString() +
                                    "\n 类型转换失败!!!! 请检查导出模板类中的类型和数据库中字段的类型是否一致" +
                                    "\n/**********************************************************************************************/");
                }
            }
        }
        return obj;
    }

    private static Map<String, Object> objectToMap(Object obj) throws Exception {
        if (obj == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();

        Field[] declaredFields = obj.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            map.put(field.getName(), field.get(obj));
        }
        return map;
    }

    public static void main(String a[]) throws Exception {
        Map<String, Object> hashMap = new HashMap<>();

        hashMap.put("name", "gaolei");
        hashMap.put("age", 56);
        hashMap.put("ID", 4534L);
        hashMap.put("score", 456D);
        hashMap.get("score");
        Stu stu;
        stu = (Stu) mapToObject(hashMap, Stu.class);
        System.out.println(stu);

        Map<String, Object> map = new HashMap<>();
        map = objectToMap(stu);
        System.out.println();
        System.out.println("adfvdaf");

    }
}